package com.bookmanager.www.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.bookmanager.www.common.utils.CommonsUtils;
import com.bookmanager.www.entity.FlRole;
import com.bookmanager.www.entity.FlUser;
import com.bookmanager.www.mapper.FlRoleMapper;
import com.bookmanager.www.mapper.FlUserMapper;
import com.bookmanager.www.service.FlUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kohgylw.kcnamer.core.KCNamer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 风离
 * @since 2021-09-17
 */
@Service
@Slf4j
public class FlUserServiceImpl extends ServiceImpl<FlUserMapper, FlUser> implements FlUserService {

    @Resource
    FlUserMapper flUserMapper;

    @Resource
    FlRoleMapper flRoleMapper;

    @Override
    public int registerAdmin(String username, String password,Integer roleId) {

        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String uid =String.valueOf(snowflake.nextId());
        int permissionsId = 0;
//        int roleId = 0;
        String avatar = "https://q4.qlogo.cn/g?b=qq&nk=1906249647&s=640";
        String description = "";
        String contactInformation = "";
        int accountStatus = 0;
        String email = "";
        String question="";
        String answer ="";

        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);

        LocalDateTime createdTimestamp = LocalDateTime.now();
        LocalDateTime updatedTimestamp = LocalDateTime.now();

        /**
         * 新注册用户角色为普通用户
         */
        int accountLevel = 8;

        /**
         * 新注册用户余额为0
         */
        BigDecimal accountBalance = new BigDecimal("999999");

        /**
         * 新注册用户默认性别为男
         */
        Integer userSex = 1;

        FlUser flUser = new FlUser(uid,permissionsId,roleId,userSex,avatar,username,encryptedPassword,
                description,contactInformation,accountStatus,email,question,answer,accountBalance,
                accountLevel,createdTimestamp,updatedTimestamp);
        int insert = flUserMapper.insert(flUser);

        return insert;
    }

    @Override
    public int registerUser(String username, String password) {

        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String uid =String.valueOf(snowflake.nextId());
        int permissionsId = 0;
        int roleId = 0;
        String avatar = "https://q4.qlogo.cn/g?b=qq&nk=1906249647&s=640";
        String description = "";
        String contactInformation = "";
        int accountStatus = 0;
        String email = "";
        String question="";
        String answer ="";

        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);

        LocalDateTime createdTimestamp = LocalDateTime.now();
        LocalDateTime updatedTimestamp = LocalDateTime.now();

        /**
         * 新注册用户角色为普通用户
         */
        int accountLevel = 0;

        /**
         * 新注册用户余额为0
         */
        BigDecimal accountBalance = new BigDecimal("0");

        /**
         * 新注册用户默认性别为男
         */
        Integer userSex = 1;

        FlUser flUser = new FlUser(uid,permissionsId,roleId,userSex,avatar,username,encryptedPassword,
                description,contactInformation,accountStatus,email,question,answer,accountBalance,
                accountLevel,createdTimestamp,updatedTimestamp);
        int insert = flUserMapper.insert(flUser);

        return insert;
    }

    @Override
    public FlUser selectUserByUsername(String username) {
        FlUser flUser =  flUserMapper.selectUserByUsername(username);
        return flUser;
    }

    @Override
    public FlUser selectUserByUsernameAndPassword(String username, String password) {
        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);
       FlUser flUser =  flUserMapper.selectUserByUsernameAndPassword(username,encryptedPassword);
        return flUser;
    }

    @Override
    public String selectRoleById(String userId) {
        Integer roleId  = flUserMapper.selectRoleIdByUserId(userId);
        if (roleId!=null)
        {
            FlRole flRole = flRoleMapper.selectRoleNameByRoleId(roleId);
//
            if (flRole!=null)
            {
                return flRole.getRoleName();
            }
        }
        return null;
    }

    @Override
    public int registerUserByEmail(String email, String password) {
        //使用默认规则实例化KCNamer工具
        KCNamer k=new KCNamer();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String uid =String.valueOf(snowflake.nextId());
        int permissionsId = 0;
        int roleId = 0;
        String avatar = "https://q4.qlogo.cn/g?b=qq&nk=1906249647&s=640";
        String description = "";
        String contactInformation = "";
        int accountStatus = 0;

        String username=k.getRandomName();
//        String email = "";
        String question="";
        String answer ="";

        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);

        LocalDateTime createdTimestamp = LocalDateTime.now();
        LocalDateTime updatedTimestamp = LocalDateTime.now();


        /**
         * 新注册用户角色为普通用户
         */
        int accountLevel = 0;

        /**
         * 新注册用户余额为0
         */
        BigDecimal accountBalance = new BigDecimal("0");

        /**
         * 新注册用户默认性别为男
         */
        Integer userSex = 1;


        FlUser flUser = new FlUser(
                uid,permissionsId,roleId,userSex,avatar,username,encryptedPassword,
                description,contactInformation,accountStatus,email,question,answer,accountBalance,
                accountLevel,createdTimestamp,updatedTimestamp
        );
        int insert = flUserMapper.insert(flUser);

        return insert;
    }

    @Override
    public int registerByPhoneNum(String phoneNum, String password) {
        //使用默认规则实例化KCNamer工具
        KCNamer k=new KCNamer();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String uid =String.valueOf(snowflake.nextId());
        int permissionsId = 0;
        int roleId = 0;
        String avatar = "https://q4.qlogo.cn/g?b=qq&nk=1906249647&s=640";
        String description = "";
//        String contactInformation = "";
        int accountStatus = 0;
        String username=k.getRandomName();
        String email = "";
        String question="";
        String answer ="";

        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);

        LocalDateTime createdTimestamp = LocalDateTime.now();
        LocalDateTime updatedTimestamp = LocalDateTime.now();


        /**
         * 新注册用户角色为普通用户
         */
        int accountLevel = 0;

        /**
         * 新注册用户余额为0
         */
        BigDecimal accountBalance = new BigDecimal("0");

        /**
         * 新注册用户默认性别为男
         */
        Integer userSex = 1;


        FlUser flUser = new FlUser(
                uid,permissionsId,roleId,userSex,avatar,username,encryptedPassword,
                description,phoneNum,accountStatus,email,question,answer,accountBalance,
                accountLevel,createdTimestamp,updatedTimestamp
        );
//        FlUser flUser = new FlUser(uid,permissionsId,roleId,avatar,username,encryptedPassword,description,phoneNum,accountStatus,email,question,answer,createdTimestamp,updatedTimestamp);
        int insert = flUserMapper.insert(flUser);

        return insert;
    }

    @Override
    public FlUser selectUserByEmailAndPassword(String username, String password) {
        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);
        FlUser flUser = flUserMapper.selectUserByEmailAndPassword(username,encryptedPassword);
        return flUser;
    }

    @Override
    public FlUser selectUserByPhoneNumAndPassword(String username, String password) {
        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);
        FlUser flUser = flUserMapper.selectUserByPhoneNumAndPassword(username,encryptedPassword);
        return flUser;
    }

    @Override
    public BigDecimal changeAccountBalanceByUserId(String userId, BigDecimal newBalance) {

        int updated = flUserMapper.changeAccountBalanceByUserId(userId,newBalance);
        if (updated != 0) {
            return flUserMapper.selectAccountBalanceByUserId(userId);
        }
        return null;
    }

    @Override
    public FlUser registerUserByOAuth2(String username, String password, String avatar, String uuid, String source, String accessToken) {
        //注册用户 并实现与第三方平台的双向绑定
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String uid =String.valueOf(snowflake.nextId());
        int permissionsId = 0;
        int roleId = 0;
        String description = "";
        String contactInformation = "";
        int accountStatus = 0;
        String email = "";
        String question="";
        String answer ="";

        //密码加密并插入数据库 密码加盐规则 ："fl1906"
        String encryptedPassword= CommonsUtils.encryptPassword(password, "fl1906");
        log.info("加密后的密码为:{}",encryptedPassword);

        LocalDateTime createdTimestamp = LocalDateTime.now();
        LocalDateTime updatedTimestamp = LocalDateTime.now();

        /**
         * 新注册用户角色为普通用户
         */
        int accountLevel = 0;

        /**
         * 新注册用户余额为0
         */
        BigDecimal accountBalance = new BigDecimal("0");

        /**
         * 新注册用户默认性别为男
         */
        Integer userSex = 1;

        FlUser flUser = new FlUser(uid,permissionsId,roleId,userSex,avatar,username,encryptedPassword,
                description,contactInformation,accountStatus,email,question,answer,accountBalance,
                accountLevel,createdTimestamp,updatedTimestamp);
        int insert = flUserMapper.insert(flUser);
        if(insert!=0)
        {
            //完成与第三方平台的双向绑定
            int insertOAuth2User = flUserMapper.insertOAuth2User(uuid,uid,source,accessToken);
            if(insertOAuth2User!=0)
            {
                return flUser;
            }
        }

        return null;
    }
}
